package com.classic.download;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.classic.base.BaseActivity;
import com.classic.duanzhi.R;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

public class AppDetailsActivity extends BaseActivity {

    public static void starThis(FragmentActivity activity, String name) {
        Intent intent = new Intent(activity, AppDetailsActivity.class);
        intent.putExtra("name", name);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_app_details;
    }

    MZBannerView banner;

    @Override
    protected void initView() {
        String name = getIntent().getStringExtra("name");
        final APP data = AppUtils.getAppDetails(name);
        banner = findViewById(R.id.banner);
        ImageView logo = findViewById(R.id.iv_logo);
        TextView appname = findViewById(R.id.tv_appName);
        TextView jianjie = findViewById(R.id.tv_jianjie);
        TextView xiazai = findViewById(R.id.tv_xiazai);
        TextView tv_v = findViewById(R.id.tv_v);
        Glide.with(this).load(data.getLogo()).into(logo);
        appname.setText(name);
        jianjie.setText(Html.fromHtml(data.getIntroduction()));
        xiazai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Uri uri = Uri.parse("market://details?id=" + data.getPackageName());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (Exception e) {
                    showTT("未找到相应的应用市场");
                }
            }
        });
        // 设置数据
        banner.setPages(data.getPatternmaking(), new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
    }

    public static class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;

        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.u_banner_layout, null);
            mImageView = (ImageView) view.findViewById(R.id.iv_show_logo);
            return view;
        }

        @Override
        public void onBind(Context context, int position, String data) {
            Glide.with(context).load(data).into(mImageView);
            // 数据绑定
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onPause() {
        super.onPause();
        banner.pause();//暂停轮播
    }

    @Override
    public void onResume() {
        super.onResume();
        banner.start();//开始轮播
    }

    @Override
    public String getActivityTitle() {
//        return "";
        return getIntent().getStringExtra("name");
    }
}
